package com.itfreer.ui.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itfreer.ui.R;
import com.itfreer.ui.base.api.IEditTextValidator;
import com.itfreer.ui.base.api.IPower;
import com.itfreer.ui.base.api.ISimpleView;
import com.itfreer.utils.PixelUtils;

public class DatePicker extends LinearLayout implements ISimpleView, IEditTextValidator, IPower {

    private String fieldName;
    private boolean readonly = false;
    private boolean required = false;
    private String validateTitle = "";
    private String validateName = "";
    private String textGravity = "";

    private com.itfreer.ui.base.datetime.DatePickerView editText;
    private TextView title;
    private TextView reqTextView;

    public DatePicker(Context context) {
        super(context);
        setView(context);
    }

    public DatePicker(Context context, AttributeSet attrs){
        super(context, attrs);
        getAttrs(context, attrs);
        setView(context);
    }

    public DatePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        setView(context);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        this.readonly = a.getBoolean(R.styleable.SimpleView_readonly, false);
        this.validateTitle = a.getString(R.styleable.SimpleView_validateTitle);
        this.validateName = a.getString(R.styleable.SimpleView_validatorName);
        this.required = a.getBoolean(R.styleable.SimpleView_required, false);
        this.readonly = a.getBoolean(R.styleable.SimpleView_readonly, false);
        this.textGravity = a.getString(R.styleable.SimpleView_textGravity);
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        this.setBackgroundColor(Color.WHITE);
        this.setGravity(Gravity.CENTER_VERTICAL);
        this.setOrientation(HORIZONTAL);
        this.setPadding(0, PixelUtils.dpToPx(context, 8), 0, PixelUtils.dpToPx(context, 8));

        title = new TextView(context);
        LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.setMarginStart(PixelUtils.dpToPx(context, 18));
        title.setLayoutParams(titleParams);
        title.setText(this.validateTitle);
        title.setTextColor(Color.BLACK);
        title.setTextSize(PixelUtils.dpToPx(getContext(), 6));
        this.addView(title);

        reqTextView = new TextView(context);
        LayoutParams reqParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        reqTextView.setLayoutParams(reqParams);
        reqTextView.setText("*");
        reqTextView.setTextColor(Color.parseColor("#FF0000"));
        this.addView(reqTextView);
        if(!required) {
            reqTextView.setVisibility(GONE);
        }

        editText = new com.itfreer.ui.base.datetime.DatePickerView(context);
        LayoutParams radioParams = new LayoutParams(0, PixelUtils.dpToPx(context, 40));
        radioParams.weight = 1;
        radioParams.rightMargin = PixelUtils.dpToPx(context, 26);
        editText.setLayoutParams(radioParams);
        editText.setFieldName(fieldName);
        editText.setValidateTitle(this.validateTitle);
        editText.setValidatorName(this.validateName);
        editText.setGravity(Gravity.RIGHT);
        editText.setBackground(null);
        this.addView(editText);
    }

    @Override
    public String getSecurityID() {
        return editText.getSecurityID();
    }

    @Override
    public void setSecurityVisible(boolean visible) {
        editText.setSecurityVisible(visible);
    }

    @Override
    public void setFieldName(String name) {
        editText.setFieldName(name);
    }

    @Override
    public String getFieldName() {
        return editText.getFieldName();
    }

    @Override
    public void setFieldValue(Object value) {
        editText.setFieldValue(value);
    }

    @Override
    public Object getFieldValue() {
        return editText.getFieldValue();
    }

    @Override
    public String getValidatorName() {
        return editText.getValidatorName();
    }

    @Override
    public void setValidatorName(String name) {
        editText.setValidatorName(name);
    }

    @Override
    public String getValidateTitle() {
        return editText.getValidateTitle();
    }

    @Override
    public void setValidateTitle(String validateTitle) {
        editText.setValidateTitle(validateTitle);
        this.validateTitle = validateTitle;
        title.setText(this.validateTitle);
    }

    public void setReadonly(boolean readonly) {
        editText.setReadonly(readonly);
    }

    public void setRequired(boolean required) {
        this.required = required;
        if(this.required) {
            reqTextView.setVisibility(VISIBLE);
        } else {
            reqTextView.setVisibility(GONE);
        }
    }

    public void setTextGravity(String textGravity) {
        this.textGravity = textGravity;
        editText.setTextGravity(textGravity);
    }
}
